<%-- 
    Document   : procesar_compra
    Created on : 16-abr-2014, 17:46:00
    Author     : romulogalindo
--%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="pe.core.db.ArcadjDto"%>
<%@page import="pe.core.db.AnuncioDto"%>
<%@page import="pe.core.db.MillionDBManager"%>
<%@page import="pe.core.MillionFactory"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="pe.db.MillionDB"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.Enumeration"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="java.io.File"%>
<%
    MillionDBManager db = new MillionDBManager();
    System.out.println("[Procesar_compra]");
    String mensaje = "NORMAL";
    String header = request.getHeader("Content-Type");
    System.out.println("[Procesar_compra] header:" + header);
    String sid = "";
    boolean ok = false;
    if (header == null) {
    } else {
//        System.out.println("[Procesar_compra] header:"+header);
        System.out.println("=================================================A");
        Enumeration enu = request.getAttributeNames();
        if (enu.hasMoreElements()) {
            System.out.println("enu:" + enu.nextElement());
        }
        System.out.println("=================================================B");
        if (header.indexOf("multipart") >= 0) {
            try {
                AnuncioDto anuncio = new AnuncioDto();
                FileItemFactory file_factory = new DiskFileItemFactory();
                ServletFileUpload servlet_up = new ServletFileUpload(file_factory);
                List items = servlet_up.parseRequest(request);
//                System.out.println("items:" + items.size());
                for (int i = 0; i < items.size(); i++) {
//                    System.out.println("item:" + items.get(i));
                    FileItem item = (FileItem) items.get(i);
                    if (!item.isFormField()) {
                        if (item.getSize() < (1024 * 1024 * 8)) {//ARCHIVOS MENORES E IGUALES A 8Mb
                            System.out.println("Trabajo sobre imagen adjunta");
                            String ext_file = item.getName().split("\\.(?=[^\\.]+$)")[1];
                            System.out.println("**::" + ext_file);
                            String name = "" + System.currentTimeMillis();
                            System.out.println("**::" + name);
                            ArcadjDto dto = db.saveImage(name + "." + ext_file);
                            anuncio.setId_arcadj(dto.getId_arcadj());
                            System.out.println("**::" + dto.getFe_arcadj());
                            String dir = "/home/romulogalindo/million";
                            System.out.println("**::" + dir);
                            //--------------------------------
                            System.out.println("Creando dir:" + dir + "/" + new SimpleDateFormat("yyyy").format(dto.getFe_arcadj()) + "/" + new SimpleDateFormat("MM").format(dto.getFe_arcadj()) + "/" + new SimpleDateFormat("dd").format(dto.getFe_arcadj()));
                            File path = new File(dir + "/" + new SimpleDateFormat("yyyy").format(dto.getFe_arcadj()) + "/" + new SimpleDateFormat("MM").format(dto.getFe_arcadj()) + "/" + new SimpleDateFormat("dd").format(dto.getFe_arcadj()));
                            if (!path.exists()) {
                                path.mkdirs();
                            }

                            File archivo = new File(path.getCanonicalPath() + "/" + dto.getId_arcadj() + "." + ext_file);
                            item.write(archivo);
                        } else {
                            mensaje = "FILE_ERROR_SIZE";
                        }
                    } else {
                        String fn = "" + item.getFieldName();
                        System.out.println(fn + ":" + item.getString());
                        if (fn.contentEquals("form_url")) {
//                            it.setTarget(item.getString());
                            anuncio.setUr_anunci(item.getString());
                        } else if (fn.contentEquals("form_titulo")) {
                            anuncio.setNo_anunci(item.getString());
                        } else if (fn.contentEquals("form_x")) {
                            anuncio.setX(Integer.parseInt(item.getString()));
                        } else if (fn.contentEquals("form_y")) {
                            anuncio.setY(Integer.parseInt(item.getString()));
                        } else if (fn.contentEquals("form_width")) {
                            anuncio.setX1(Integer.parseInt(item.getString()));
                        } else if (fn.contentEquals("form_height")) {
                            anuncio.setY1(Integer.parseInt(item.getString()));
                        } else if (fn.contentEquals("form_comentario")) {
                            anuncio.setDescripcion(item.getString());
                        }
                    }

                }
                System.out.println("=========================================B1:>" + anuncio.getId_arcadj());
                if (anuncio.getId_arcadj() > 0) {
                    anuncio.setCo_bloini(db.getPosicion(anuncio.getX(), anuncio.getY()));
                    anuncio.setCo_blofin(db.getPosicion(anuncio.getX() + anuncio.getX1() - 1, anuncio.getY() + anuncio.getY1() - 1));
                    ok = db.saveAnuncio(anuncio);

                }
            } catch (Exception ep) {
                mensaje = "FILE_ERROR";
                System.out.println("e>" + ep.getMessage());
                ep.printStackTrace();
            }
        }
        System.out.println("=================================================C");
        if (ok) {
            System.out.println("Iniciando....");
//        sce.getServletContext().
            MillionFactory.home(request.getServletContext().getInitParameter("million_home_image"));
            MillionDBManager dao = new MillionDBManager();//provee objetos de la base de datos
            try {
                MillionFactory.procesar(dao.getAnuncios());
            } catch (Exception ex) {
                //Logger.getLogger(DeathStinger.class.getName()).log(Level.SEVERE, null, ex);
            }
            dao.close();
            System.out.println("Listo!");
//            response.sendRedirect("compra?id=" + sid);
            response.sendRedirect("/million");
        } else {
            response.sendRedirect("compra");
        }
    }
%>